package com.jk.kdd.console.service;

import com.jk.kdd.console.dao.SystemVariableClassifyMapper;
import com.jk.kdd.console.model.SystemVariable;
import com.jk.kdd.console.model.SystemVariableClassify;
import com.jk.kdd.console.vo.SystemVariableClassifyVO;
import com.jk.kdd.system.model.SystemUser;
import com.jk.util.BaseDataUtil;
import org.smart4j.framework.helper.BeanHelper;
import org.smart4j.framework.util.CastUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * @author hanping.fu
 * @project jk-console
 * @data 2017/03/07
 * @desc
 */
@Service("systemVariableClassifyService")
public class SystemVariableClassifyServiceImpl implements SystemVariableClassifyService{

    @Resource
    private SystemVariableClassifyMapper systemVariableClassifyMapper;



    /**
     * 变量分类列表
     */
    @Override
    public List<SystemVariableClassifyVO> selectByCondition() {

        List<SystemVariableClassify> systemVariableClassifyList = systemVariableClassifyMapper.selectByCondition();

        List<SystemVariableClassifyVO> systemVariableClassifyVOList = BeanHelper.mapList(systemVariableClassifyList, SystemVariableClassifyVO.class);

        return systemVariableClassifyVOList;

    }


    /**
     * 查询出最大的id
     */
    @Override
    public Long selectMaxId() {

        Long maxId = systemVariableClassifyMapper.selectMaxId();
        return maxId;
    }


    /**
     * 创建总分类
     */
    @Override
    @Transactional
    public boolean createVariable(String classifyId,String classifyName,  String operateId) {

        //分别通过classifyId和classifyName查询是否存在记录
        SystemVariableClassify avc = systemVariableClassifyMapper.selectByClassifyId(classifyId);
        SystemVariableClassify svc = systemVariableClassifyMapper.selectByClassifyName(classifyName);

       if(avc!=null || svc!=null){

           return false;
       }

        //查询出最大的id
        Long maxId = systemVariableClassifyMapper.selectMaxId();
        Long maxIdNext = maxId + 1;
        SystemVariableClassify systemVariableClassify = new SystemVariableClassify();
        systemVariableClassify.setSerialNumber(Integer.parseInt(maxIdNext.toString()));
        systemVariableClassify.setClassifyId(classifyId);
        systemVariableClassify.setClassifyName(classifyName);
        BaseDataUtil.setBaseDataForEntity(systemVariableClassify, "1", operateId);
        systemVariableClassifyMapper.insertSelective(systemVariableClassify);

        return true;
    }

    /**
     * 修改总分类
     */
    @Override
    @Transactional
    public boolean modifyVariable(String id,String classifyId,String classifyName,String operateId) {

        SystemVariableClassify systemVariableClassify = new SystemVariableClassify();
        systemVariableClassify.setId(CastUtil.castLong(id));
        systemVariableClassify.setClassifyId(classifyId);
        systemVariableClassify.setClassifyName(classifyName);
        // 设置基础数据
        SystemVariableClassify tempSystemVariableClassify = systemVariableClassifyMapper.selectByPrimaryKey(systemVariableClassify.getId());
        systemVariableClassify.setUpdateCount(tempSystemVariableClassify.getUpdateCount());
        BaseDataUtil.setBaseDataForEntity(systemVariableClassify, "2", operateId);
        systemVariableClassifyMapper.updateByPrimaryKeySelective(systemVariableClassify);

        return true;
    }

    /**
     * 删除总分类
     */
    @Override
    @Transactional
    public boolean deleteVariable(String id, String operateId) {

        SystemVariableClassify systemVariableClassify = new SystemVariableClassify();
        systemVariableClassify.setId(CastUtil.castLong(id));

        systemVariableClassify = systemVariableClassifyMapper.selectByPrimaryKey(systemVariableClassify.getId());
        BaseDataUtil.setBaseDataForEntity(systemVariableClassify, "3", operateId);
        systemVariableClassifyMapper.updateByPrimaryKeySelective(systemVariableClassify);
        return true;
    }

    /**
     * 变量分类列表排序
     */
    @Override
    @Transactional
    public boolean sortVariableList(String id1, String id2, String sNum1, String sNum2, String operateId) {

        SystemVariableClassify systemVariableClassify1 = systemVariableClassifyMapper.selectByPrimaryKey(CastUtil.castLong(id1));
        systemVariableClassify1.setSerialNumber(Integer.parseInt(sNum2));
        BaseDataUtil.setBaseDataForEntity(systemVariableClassify1, "2", operateId);
        systemVariableClassifyMapper.updateByPrimaryKeySelective(systemVariableClassify1);

        SystemVariableClassify systemVariableClassify2 = systemVariableClassifyMapper.selectByPrimaryKey(CastUtil.castLong(id2));
        systemVariableClassify2.setSerialNumber(Integer.parseInt(sNum1));
        BaseDataUtil.setBaseDataForEntity(systemVariableClassify2, "2", operateId);
        systemVariableClassifyMapper.updateByPrimaryKeySelective(systemVariableClassify2);

        return true;
    }


}
